﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.BasicInformation.warehouses;
using THE_WIND_RISES.Common.Dtos;

namespace Enterprise_Set.API.Application.Commands.Week1.Warehouse_CRUD
{
    /// <summary>
    /// 修改仓库表数据
    /// </summary>
    public class UpWarehouseHandler : IRequestHandler<UpWarehouseCommand, Result>
    {
        public UpWarehouseHandler(IBaseRepository<Warehouse> warehouseRepository, IMapper mapper)
        {
            WarehouseRepository = warehouseRepository;
            this.mapper=mapper;
        }

        public IBaseRepository<Warehouse> WarehouseRepository { get; set; }
        public IMapper mapper { get; set; }
        public async Task<Result> Handle(UpWarehouseCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var UpData = mapper.Map<Warehouse>(request);
                var result = new Result() { Code = (int)CommonErrorCodes.Update };
                var sql = "update Warehouse setWarehousNumBer=@setWarehousNumBer," +
                          "WarehouseName=@WarehouseName,WarehouseAddr=@WarehouseAddr," +
                          "WarehouseRemark=@WarehouseRemark,WarehouseState=@WarehouseState," +
                          "WarehouseIsDel=@WarehouseIsDel,Handle=@Handle,HandleContext=@HandleContext," +
                          "HandleTime=@HandleTime,Pid=@Pid,IsDelete=@IsDelete where WarehouseId=@WarehouseId ;";
                var UpNum = WarehouseRepository.Update(sql, new
                {
                    WarehouseId = UpData.WarehouseId,
                    WarehousNumBer = UpData.WarehousNumBer,
                    WarehouseName = UpData.WarehouseName,
                    WarehouseAddr = UpData.WarehouseAddr,
                    WarehouseRemark = UpData.WarehouseRemark,
                    WarehouseState = UpData.WarehouseState,
                    WarehouseIsDel = UpData.WarehouseIsDel,
                    Handle = UpData.Handle,
                    HandleContext = UpData.HandleContext,
                    HandleTime = UpData.HandleTime,
                    Pid = UpData.Pid,
                    IsDelete = UpData.IsDelete
                });
                if (UpNum > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    return result;
                }
                else
                {
                    return result;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
